const commonFn = {
  // 截取字符串的首尾空白符
  strTrim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
  },
  /*数组去重*/
  unique(arr) {
    var result = [], hash = {};
    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
      if (!hash[elem]) {
        result.push(elem);
        hash[elem] = true;
      }
    }
    return result;
  },
  j2s(obj) {
    return JSON.stringify(obj)
  },
  shallowRefresh(name) {
    router.replace({ path: '/home/refresh', query: { name: name } })
  },
  closeGlobalLoading() {
    setTimeout(() => {
      store.dispatch('showLoading', false)
    }, 0)
  },
  openGlobalLoading() {
    setTimeout(() => {
      // store.dispatch('showLoading', true)
    }, 0)
  },
  cloneJson(obj) {
    return JSON.parse(JSON.stringify(obj))
  },
  toastMsg(type, msg) {
    switch (type) {
      case 'normal':
        bus.$message(msg)
        break
      case 'success':
        bus.$message({
          message: msg,
          type: 'success'
        })
        break
      case 'warning':
        bus.$message({
          message: msg,
          type: 'warning'
        })
        break
      case 'error':
        bus.$message.error(msg)
        break
    }
  },

  /**
   * @name 格式化时间
   * @param {*} time 
   * @param {str} str
   */
  formatTime(time, str) {
    new Date().getMinutes()
    if (!time) {
      return null
    }
    if(time.length==10||time.length==7){return time}
    if (str == "month") {return time.getFullYear() + '-' + ((time.getMonth() + 1)<10?"0"+(time.getMonth() + 1):(time.getMonth() + 1))}
    if(str=="time"){return time.getFullYear() + '-' + ((time.getMonth() + 1)<10?"0"+(time.getMonth() + 1):(time.getMonth() + 1))+'-'+( time.getDate()<10?"0"+time.getDate():time.getDate())+" "+(time.getHours()<10?"0"+time.getHours():time.getHours())+":"+(time.getMinutes()<10?"0"+time.getMinutes():time.getMinutes())+":"+time.getSeconds()}
    // + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); 
    return time.getFullYear() + '-' + ((time.getMonth() + 1)<10?"0"+(time.getMonth() + 1):(time.getMonth() + 1)) + '-' +( time.getDate()<10?"0"+time.getDate():time.getDate())
    
  },

    /**
   * @name 获取当前路由path
   * @param {*} vue 
   */
  getRouterPath(vue) {
    var routerPath = vue.$router.history.current.path
    return routerPath
  },

  /**
   * @name 获取当前路由中携带的参数，返回param或者返回全部的参数
   * @param {*} vue 
   * @param {str} param 
   */
  getRouterParams(vue, param) {
    if (param) {
      return vue.$router.history.current.params[param]
    } else {
      return vue.$router.history.current.params
    }
  },

  //生成随机字符串
  randomString(len) {
    len = len || 32;
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var maxPos = chars.length;
    var str = '';
    for (let i = 0; i < len; i++) {
      str += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return str;
  },

  apiPost(url, data,bool) {
    _g.openGlobalLoading()
    url = PATH + url
    data = !bool?qs.stringify(data):data
    return new Promise((resolve, reject) => {
      axios.post(url, data).then((response) => {
        _g.closeGlobalLoading()
        resolve(response.data)
      }).catch((response) => {
        _g.closeGlobalLoading()
        resolve(response)
        bus.$message({
          message: '请求超时，请检查网络',
          type: 'warning'
        })
      })
    })
  },
}

export default commonFn
